package com.itlong.cloud.property.service.impl;

import com.itlong.cloud.POJO.DTO.property.PaymentRecordDTO;
import com.itlong.cloud.POJO.VO.app.AppElevatorMealPayRecordInfoVO;
import com.itlong.cloud.POJO.VO.app.AppElevatorMealPayRecordVO;
import com.itlong.cloud.POJO.VO.property.PropertyPaymentRecordVO;
import com.itlong.cloud.property.dao.IPropertyPaymentDao;
import com.itlong.cloud.property.service.IPropertyPaymentService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      物业缴费管理服务层接口实现类
 * </desc>
 *
 * @createDate2020/7/15
 **/
@Service
public class PropertyPaymentServiceImpl implements IPropertyPaymentService {

    @Autowired
    private IPropertyPaymentDao iPropertyPaymentDao;

    /**
     * <desc>
     *      分页获取缴费记录数据
     * </desc>
     *
     * @param paymentRecordDTO 缴费记录DTO
     * @return
     * @Author Zhu.yj
     * @createDate  2020/7/15
     */
    @Override
    public Page<PropertyPaymentRecordVO> getPage(PaymentRecordDTO paymentRecordDTO) throws Exception{
        if (paymentRecordDTO.getOrderColumn() == null || paymentRecordDTO.getOrderColumn() == ""){
            paymentRecordDTO.setOrderColumn("createTime");
            paymentRecordDTO.setOrderRule("DESC");
        }
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(paymentRecordDTO);
        List<String> recordType = StringHandlerUtil.splitStringList(paymentRecordDTO.getRecordType());
        List<String> paymentType = StringHandlerUtil.splitStringList(paymentRecordDTO.getPaymentType());
        Page<PropertyPaymentRecordVO> page = new Page<>();
        page.getPage().setCurrentPage(paymentRecordDTO.getCurrentPage());
        page.getPage().setPageSize(paymentRecordDTO.getPageSize());
        params.put("startSlip", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        params.put("recordType", recordType);
        params.put("paymentType", paymentType);
        page.setRows(iPropertyPaymentDao.getPage(params), iPropertyPaymentDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     *     获取乘梯缴费记录列表
     * </desc>
     *
     * @param projectId 项目编号
     * @param roomId 房间ID
     * @param pageNum 请求页(每页15条)
     * @param consumeTime 缴费记录DTO
     * @return MessageVO 受影响行数
     * @author Qiang.S
     * @createDate 2020/07/16
     */
    @Override
    public List<AppElevatorMealPayRecordVO> getLiftPayRecord(String projectId, String roomId,
                                                    String pageNum, String consumeTime) throws Exception{
        Map<String, Object> params = new HashMap<>();
        Page<PropertyPaymentRecordVO> page = new Page<>();
        page.getPage().setPageSize("15");
        page.getPage().setCurrentPage(pageNum);
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        params.put("projectId",projectId);
        params.put("roomId",roomId);
        consumeTime = "".equals(consumeTime)?null:consumeTime;
        params.put("consumeTime",consumeTime);
        List<AppElevatorMealPayRecordVO> list = new ArrayList<>();
        //查询时间列表
        List<PropertyPaymentRecordVO> dateList = iPropertyPaymentDao.getPayMonthByRoom(params);
        if (dateList.size()!=0){
        for (int i = 0; i < dateList.size(); i++) {
            PropertyPaymentRecordVO voRecord = dateList.get(i);
            AppElevatorMealPayRecordVO recordVO = new AppElevatorMealPayRecordVO();
            if (voRecord!=null) {
                if (voRecord.getCreateTime() == null || voRecord.getPaymentId() == null) {//防止为空报错直接跳出
                    break;
                }
                recordVO.setMonth(voRecord.getCreateTime());
                String[] paymentIds = voRecord.getPaymentId().split(",");
                recordVO.setPayList(iPropertyPaymentDao.getPayMonthByPaymentId(paymentIds));
                list.add(recordVO);
            }
         }
        }
        return list;
    }

    /**
     * <desc>
     *      获取缴费记录总金额
     * </desc>
     *
     * @param paymentRecordDTO 参数实体
     * @return
     * @Author Zhu.yj
     * @createDate  2020/7/20
     */
    @Override
    public String getPaymentPriceCount(PaymentRecordDTO paymentRecordDTO) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(paymentRecordDTO);
        List<String> recordType = StringHandlerUtil.splitStringList(paymentRecordDTO.getRecordType());
        List<String> paymentType = StringHandlerUtil.splitStringList(paymentRecordDTO.getPaymentType());
        params.put("recordType", recordType);
        params.put("paymentType", paymentType);
        return iPropertyPaymentDao.getPaymentPriceCount(params);
    }

    /**
     * <desc>
     *      检查活动套餐是否在当前房间使用过
     * </desc>
     *
     * @param mealId 套餐编号
     * @param roomId 房间ID
     * @return
     * @Author Zhu.yj
     * @createDate  2020/7/20
     */
    @Override
    public Integer checkMealInfo(String mealId, String roomId) throws Exception {
        return iPropertyPaymentDao.checkMealInfo(mealId,roomId);
    }

    /**
     * <desc>
     *      检查套餐是否禁用
     * </desc>
     *
     * @param mealId 套餐编号
     * @return
     * @Author Zhu.yj
     * @createDate  2020/7/20
     */
    @Override
    public Integer checkMealStatus(String mealId) throws Exception {
        return iPropertyPaymentDao.checkMealStatus(mealId);
    }
}
